// Include system file
#include "r_cg_can.h"

// Include project file
#include "CAN.h"
#include "fet.h"
#include "nvm.h"
#include "control.h"
#include "faults.h"
#include "Main.h"
#include "Switches.h"
#include "Joystick.h"
#include "Serial.h"
#include "Battery.h"
#include "BMS_CAN.h"
#include "TransportCtrlLogic.h"
#include "RangeExtender.h"
structTransportInput stTransportInput = {0};
structTransportOutput stTransportOutput = {0};

extern void vControlInput(void);
extern void vControlOutput(void);
extern UINT16  u16CaculateRpm(UINT8 u8DiValue);
void vPorcCtrlLogic(void)//1ms
{
	static UINT8 u8_10msCnt=0,u8_5msCnt = 0;
	if(++u8_5msCnt>=5){u8_5msCnt=0;}
	if(++u8_10msCnt>=10){u8_10msCnt=0;}

	switch(u8_5msCnt)
	{
		case 1:
			vControlInput();
		break;
		default:
		break;
	}
	switch(u8_10msCnt)//10ms
	{
		case 1:
			vProcRangeExtenderManualMode();//10ms
		break;
		case 2:
		break;
		case 3:
		break;
		case 4:
		break;
		case 5:
			vControlOutput();
		break;
		default:break;
	}
}
#define TemeSensorADC_CH	(1)
#define TempSensorP_FBK_ADC_CH	(2)
void vControlInput(void)
{
	switches();
	stTransportInput.unDigitalInput.bit.fRelayFeedBack = SWITCH_Data.wSwitches.sw2.f1A;
	stTransportInput.unDigitalInput.bit.fSafeEdge = SWITCH_Data.wSwitches.sw2.f12A;
	stTransportInput.unDigitalInput.bit.fOilPressure = SWITCH_Data.wSwitches.sw2.f5C;
	
	stTransportInput.unAnalogInput.bit.u16PumpRpm = u16CaculateRpm(SWITCH_Data.wSwitches.sw2.f5B);
	stTransportInput.unAnalogInput.bit.u16TempSensor = u16GetADCResult(TemeSensorADC_CH);
	stTransportInput.unAnalogInput.bit.u16TempSensorP_FBK = u16GetADCResult(TempSensorP_FBK_ADC_CH);
	 
}
UINT16  u16CaculateRpm(UINT8 u8DiValue)
{
	UINT16 u16Rpm=0;
	return u16Rpm;
}
void vControlOutput(void)
{
	/*输出赋值*/
/*
	struct
	{
		UINT8 fStartCoil:1;
		UINT8 fHoldCoil:1;
		UINT8 fWaterPump:1;
		UINT8 fOilPump:1;
		UINT8 fChargeCtrlCoil:1;
		UINT8 fPreHeatCoil:1;
		UINT8 Byte0NC:2;
	}bit;
	*/
	stTransportOutput.unDigitalOutput.bit.fStartRelay = unRE_Out.bit.fStartCoil;
	stTransportOutput.unDigitalOutput.bit.fStopHoldCtrl = unRE_Out.bit.fHoldCoil;
	stTransportOutput.unDigitalOutput.bit.fOilPumpCtrl = unRE_Out.bit.fOilPump;
	stTransportOutput.unDigitalOutput.bit.fWaterPumpRelay = unRE_Out.bit.fWaterPump;
	stTransportOutput.unDigitalOutput.bit.fChargeRelay = unRE_Out.bit.fChargeCtrlCoil;
	//
	/*
	stTransportOutput.unAnalogOutput.Bytes[0] = 0;
	stTransportOutput.unDigitalOutput.Bytes[0] = 0;
	stTransportOutput.unDigitalOutput.Bytes[1] = 0;
	*/
	vSetStatus2A((stTransportOutput.unDigitalOutput.bit.fLeftOutValve) ? 1 : 0);
	vSetStatus3A((stTransportOutput.unDigitalOutput.bit.fChargeRelay) ? 1 : 0);
	vSetDuty6A((stTransportOutput.unDigitalOutput.bit.fStartRelay) ? 100 : 0);
	vSetDuty7A((stTransportOutput.unDigitalOutput.bit.fStopHoldCtrl) ? 100 : 0);
	vSetStatus8A((stTransportOutput.unDigitalOutput.bit.fWaterPumpRelay) ? 1 : 0);
	vSetDuty6B((stTransportOutput.unDigitalOutput.bit.fOilPumpCtrl) ? 100 : 0);
	vSetDuty7B((stTransportOutput.unDigitalOutput.bit.fPreHeatCtrl) ? 100 : 0);
	vSetStatus8B((stTransportOutput.unDigitalOutput.bit.fHorn) ? 1 : 0);
	vSetDuty6C((stTransportOutput.unDigitalOutput.bit.fLeftInValve) ? 100 : 0);
	vSetStatus7C((stTransportOutput.unDigitalOutput.bit.fRightOutValve) ? 1 : 0);
	vSetStatus8C((stTransportOutput.unDigitalOutput.bit.fMotionBeep) ? 1 : 0);
	vSetStatus9C((stTransportOutput.unDigitalOutput.bit.fRightInValve) ? 1 : 0);
	vSetStatus9B((stTransportOutput.unDigitalOutput.bit.fLampCtrl) ? 1 : 0);

	vSetDuty4C(stTransportOutput.unAnalogOutput.bit.u8TempSeneorP);
}
